package com.css.dzj.dzjfxyj.modules.over.service.impl;

import com.css.dzj.dzjfxyj.common.util.Constant;
import com.css.dzj.dzjfxyj.common.util.HttpClientUtil;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.common.util.ServiceUtil;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.acquier.entity.QJgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.distribute.entity.JgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.distribute.entity.QJgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.over.mapper.OverMapper;
import com.css.dzj.dzjfxyj.modules.over.service.OverServcie;
import com.css.dzj.dzjfxyj.modules.reply.entitiy.JgFxyjFeedbackEntity;
import com.css.dzj.dzjfxyj.modules.reply.entitiy.QJgFxyjFeedbackEntity;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OverServiceImpl implements OverServcie {
    @Autowired
    OverMapper overMapper;
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Override
    public QueryResults<JgFxyjWarningSignalEntity> getPage(Pageable pageable, String fxyjDomainId, String fxyjTitle, String fxyjLevel, String fxyjObjectName, String startTime, String endTime, String startTime1, String endTime1) {

        QJgFxyjWarningSignalEntity q=QJgFxyjWarningSignalEntity.jgFxyjWarningSignalEntity;
        Predicate predicate=q.isNotNull().or(q.isNull());
        predicate = (fxyjDomainId == null || fxyjDomainId.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.fxyjDomainId.eq(fxyjDomainId.trim()));
        predicate = (fxyjTitle == null || fxyjTitle.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.fxyjTitle.like("%" + fxyjTitle.trim() + "%"));
        predicate = (fxyjLevel == null || fxyjLevel.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.fxyjLevel.eq(fxyjLevel));
        predicate = (fxyjObjectName == null || fxyjObjectName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.fxyjObjectName.like("%"+fxyjObjectName.trim()+"%"));
        if((StringUtils.isEmpty(startTime))&&(!StringUtils.isEmpty(endTime))){
            Date startDate = null;
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                startDate = sf.parse("2010-12-30");
                endDate = sf.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.fxyjTransmitTime.loe(endDate));
        }
        if((!StringUtils.isEmpty(startTime))&&(StringUtils.isEmpty(endTime))){
            Date startDate = null;
            Date endDate=new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                startDate = sf.parse(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.fxyjTransmitTime.between(startDate,endDate));
        }
        if((!StringUtils.isEmpty(startTime))&&(!StringUtils.isEmpty(endTime))){
            Date startDate = null;
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            try {
                startDate = sf.parse(startTime);
                endDate = sf.parse(endTime);
                c.setTime(endDate);
                int day = c.get(Calendar.DATE);
                c.set(Calendar.DATE,day+1);
                endDate = c.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.fxyjTransmitTime.between(startDate,endDate));
        }

        if((StringUtils.isEmpty(startTime1))&&(!StringUtils.isEmpty(endTime1))){
            Date startDate = null;
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                startDate = sf.parse("2010-12-30");
                endDate = sf.parse(endTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.dispenseTime.loe(endDate));
        }
        if((!StringUtils.isEmpty(startTime1))&&(StringUtils.isEmpty(endTime1))){
            Date startDate = null;
            Date endDate=new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                startDate = sf.parse(startTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.dispenseTime.between(startDate,endDate));
        }
        if((!StringUtils.isEmpty(startTime1))&&(!StringUtils.isEmpty(endTime1))){
            Date startDate = null;
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            try {
                startDate = sf.parse(startTime1);
                endDate = sf.parse(endTime1);
                c.setTime(endDate);
                int day = c.get(Calendar.DATE);
                c.set(Calendar.DATE,day+1);
                endDate = c.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.dispenseTime.between(startDate,endDate));
        }

        return jpaQueryFactory.selectFrom(q).
                where(predicate,q.getState.eq(Constant.YIGET),q.revertState.eq(Constant.DAIREPLY),q.dispenseState.eq(Constant.YIDISTRIBUTE)).
                orderBy(q.createTime.desc()).
                offset(pageable.getOffset()).
                limit(pageable.getPageSize()).
                fetchResults();

    }
    //结束操作
    @Override
    @Transactional
    public R saveOver(JgFxyjFeedbackEntity jgFxyjFeedbackEntity, String id,HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(jgFxyjFeedbackEntity)) {
                throw new RuntimeException("新增不能为空");
            }
            List<JgFxyjFenfaEntity> jgFxyjFenfaEntity=findDistrById(id);
            //jgFxyjFeedbackEntity.setCreatePreson(ServiceUtil.getLoginName(request));
            jgFxyjFeedbackEntity.setCreateTime(new Date());
            //存储分办Id
            jgFxyjFeedbackEntity.setFbId(jgFxyjFenfaEntity.get(0).getId());
            //更新主表的处置状态和结束状态
            update(id);
            overMapper.save(jgFxyjFeedbackEntity);
            return R.ok();
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //根据主表id查找主表信息
    @Override
    public JgFxyjWarningSignalEntity findById(String id) {
        QJgFxyjWarningSignalEntity q=QJgFxyjWarningSignalEntity.jgFxyjWarningSignalEntity;
        return jpaQueryFactory.selectFrom(q).where(q.id.eq(id)).fetchOne();
    }
    //根据主表Id查找分办信息表
    @Override
    public List<JgFxyjFenfaEntity> findDistrById(String id) {
        QJgFxyjFenfaEntity q=QJgFxyjFenfaEntity.jgFxyjFenfaEntity;
        return jpaQueryFactory.selectFrom(q).where(q.getId.eq(id)).fetch();
    }
    //根据分办表Id查找结束信息
    @Override
    public JgFxyjFeedbackEntity findFeebackById(String id) {
        QJgFxyjFeedbackEntity q=QJgFxyjFeedbackEntity.jgFxyjFeedbackEntity;
        return jpaQueryFactory.selectFrom(q).where(q.fbId.eq(id)).fetchOne();
    }
    //更新主表的处置状态和结束状态
    @Override
    @Transactional
    public R update(String id) {
        QJgFxyjWarningSignalEntity q=QJgFxyjWarningSignalEntity.jgFxyjWarningSignalEntity;
        jpaQueryFactory.update(q).set(q.overState,Constant.YIOVER).set(q.dealState,Constant.YIJIESHU).where(q.id.eq(id)).execute();
        return R.ok();
    }


}
